﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Security;
using ScrumTable.Common.Threading;
using ScrumTable.DL.Data.Excel.Cache;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Schema.ScrumTableV1;

#endregion

namespace ScrumTable.DL.Data.Excel.Services
{
    /// <summary>
    /// This class is used to initiate the connection and load all needed
    /// information to access data on the project level.
    /// </summary>
    internal class ConnectionCacheLoader
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly AsyncCallback _callback;
        private readonly ConnectionInformationManager _connectionCtx;
        private readonly string _moniker;
        private readonly bool _loadAsync;
        private readonly ScrumTableV1Schema _schema;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new ConnectionCacheLoader instance, no public constructor
        /// needed.
        /// </summary>
        internal ConnectionCacheLoader(
            ConnectionInformationManager connectionCtx,
            string moniker,
            ScrumTableV1Schema schema,
            AsyncCallback callback,
            bool loadAsync)
        {
            PreCondition.AssertNotNullOrEmpty(moniker, "moniker");
            PreCondition.AssertNotNull(connectionCtx, "connectionCtx");
            PreCondition.AssertNotNull(callback, "callback");
            PreCondition.AssertNotNull(schema, "schema");

            _loadAsync = loadAsync;
            _moniker = moniker;
            _connectionCtx = connectionCtx;
            _callback = callback;
            _schema = schema;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Loads the data which is required to connect to the back end tfs
        /// system.
        /// </summary>
        public void LoadData()
        {
            if (_loadAsync)
            {
                new AsyncCall<AsyncCallback>(LoadDataAsync).Execute(_callback);
            }
            else
            {
                LoadDataAsync(_callback);
            }
        }

        private void LoadDataAsync(AsyncCallback callback)
        {
            try
            {
                CacheContext cacheContext = new CacheContext(_schema);
                DataProvider cacheDataProvider = new DataProvider(
                    cacheContext,
                    _connectionCtx);
                cacheDataProvider.LoadProjects(_moniker);

                cacheContext.Import(cacheDataProvider.Projects);
                cacheContext.SetDataProvider(cacheDataProvider);

                callback(new DataResult<CacheContext>(cacheContext));
            }
            catch (Exception e)
            {
                callback(new DataResult<CacheContext>(null, e));
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}